/**
 * Copyright (C) 2010 Telecom Digital
 */
package org.achartengine.chart;

import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;

/**
 * The range bar chart rendering class.
 */
public class CandlestickChart extends BarChart
{
	private static final String	TAG	= CandlestickChart.class
										.getSimpleName();

	/**
	 * Builds a new range bar chart instance.
	 * 
	 * @param dataset the multiple series dataset
	 * @param renderer the multiple series renderer
	 * @param type the range bar chart type
	 */
	public CandlestickChart(XYMultipleSeriesDataset dataset,
			XYMultipleSeriesRenderer renderer, Type type)
	{
		super(dataset, renderer, type);
	}

	/**
	 * The graphical representation of a series.
	 * 
	 * @param canvas the canvas to paint to
	 * @param paint the paint to be used for drawing
	 * @param points the array of points to be used for drawing the series
	 * @param seriesRenderer the series renderer
	 * @param yAxisValue the minimum value of the y axis
	 * @param seriesIndex the index of the series currently being drawn
	 */
	public void drawSeries(Canvas canvas, Paint paint, float[] points,
			SimpleSeriesRenderer seriesRenderer, float yAxisValue,
			int seriesIndex)
	{
		TDStockLog.d(TAG, "#drawSeries::seriesIndex=" + seriesIndex);
		int seriesNr = mDataset.getSeriesCount();
		TDStockLog.d(TAG, "#drawSeries::seriesNr=" + seriesNr);
		int length = points.length;
		//����ͼ����ɫ
		//paint.setColor(seriesRenderer.getColor());
		//����ʵ��ͼ��
		if (seriesIndex == 0)
		{
			paint.setStyle(Style.FILL);
			if (seriesNr == 3)
			{
				seriesNr = 1;
			}
			float halfDiffX = getHalfDiffX(points, length, seriesNr);
			for (int i = 0; i < length; i += 8)
			{
				float xOpen = points[i];
				float yOpen = points[i + 1];
				// xMin = xMax
				float xClose = points[i + 2];
				float yClose = points[i + 3];

				float xLow = points[i + 4];
				float yLow = points[i + 5];

				float xHigh = points[i + 6];
				float yHigh = points[i + 7];

				//if (mType == Type.STACKED) {
				//  canvas.drawRect(xOpen - halfDiffX, yClose, xClose + halfDiffX, yOpen, paint);
				//} else {
				float startX = xOpen - seriesNr * halfDiffX + seriesIndex
						* 2 * halfDiffX;
				paint.setColor(Color.WHITE);
				canvas.drawLine(xLow, yLow, xHigh, yHigh, paint);
				if (yClose <= yOpen)
					paint.setColor(Color.GREEN);
				else
					paint.setColor(Color.RED);
				canvas.drawRect(startX, yClose, startX + 2 * halfDiffX,
						yOpen, paint);

				//}
			}
		}
		else
		{
			//XYSeriesRenderer renderer = new XYSeriesRenderer();
			XYSeriesRenderer renderer = (XYSeriesRenderer) seriesRenderer;
			renderer.setFillBelowLine(false);
			renderer.setFillPoints(true);
			/*if(seriesIndex == 1) {
				   
				   renderer.setPointStyle(PointStyle.POINT);
				   renderer.setColor(Color.GREEN);
				   renderer.setFillPoints(true);
			}else {
				 renderer.setPointStyle(PointStyle.POINT);
			   renderer.setColor(Color.YELLOW);
			   renderer.setFillPoints(true);
				
			}*/

			paint.setStyle(Style.FILL);
			float lineWidth = paint.getStrokeWidth();
			//paint.setStrokeWidth(renderer.getLineWidth());
			if (renderer.isFillBelowLine())
			{
				// paint.setColor(renderer.getFillBelowLineColor());
				int pLength = points.length;
				float[] fillPoints = new float[pLength + 4];
				System.arraycopy(points, 0, fillPoints, 0, length);
				fillPoints[0] = points[0] + 1;
				fillPoints[length] = fillPoints[length - 2];
				fillPoints[length + 1] = yAxisValue;
				fillPoints[length + 2] = fillPoints[0];
				fillPoints[length + 3] = fillPoints[length + 1];
				//paint.setStyle(Style.FILL);
				drawPath(canvas, fillPoints, paint, true);
			}
			// paint.setColor(seriesRenderer.getColor());
			paint.setColor(seriesIndex == 1 ? Color.GREEN : Color.YELLOW);
			paint.setStyle(Style.STROKE);
			drawPath(canvas, points, paint, false);
			paint.setStrokeWidth(lineWidth);
			//   paint.setStrokeWidth(lineWidth);

			if (seriesIndex == 1)
			{
				paint.setColor(Color.GREEN);
				paint.setTextSize(12);
				canvas.drawText("-MA10", canvas.getWidth() - 80, 20, paint);
				paint.setColor(Color.YELLOW);
				canvas.drawText("-MA50", canvas.getWidth() - 40, 20, paint);
			}

		}

	}

	/**
	 * The graphical representation of the series values as text.
	 * 
	 * @param canvas the canvas to paint to
	 * @param series the series to be painted
	 * @param paint the paint to be used for drawing
	 * @param points the array of points to be used for drawing the series
	 * @param seriesIndex the index of the series currently being drawn
	 */
	protected void drawChartValuesText(Canvas canvas, XYSeries series,
			Paint paint, float[] points, int seriesIndex)
	{
		TDStockLog.d(TAG, "#drawChartValuesText::seriesIndex=" + seriesIndex);
		if (seriesIndex == 0)
		{
			int seriesNr = mDataset.getSeriesCount();
			float halfDiffX = getHalfDiffX(points, points.length, seriesNr);
			for (int k = 0; k < points.length; k += 4)
			{
				float x = points[k];
				if (mType == Type.DEFAULT)
				{
					x += seriesIndex * 2 * halfDiffX - (seriesNr - 1.5f)
							* halfDiffX;
				}
				// draw the maximum value
				drawText(canvas, getLabel(series.getY(k / 2 + 1)), x,
						points[k + 3] - 3f, paint, 0);
				// draw the minimum value
				drawText(canvas, getLabel(series.getY(k / 2)), x,
						points[k + 1] + 7.5f, paint, 0);
			}
		}

	}

	/**
	 * Returns the value of a constant used to calculate the half-distance. 
	 * @return the constant value
	 */
	protected float getCoeficient()
	{
		return 0.5f;
	}

}
